package com.indexingsystem.db.mongoDB.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;

import com.alibaba.fastjson.JSON;
import com.indexingsystem.boss.Vo.ResourceCategorsVo;
import com.indexingsystem.boss.Vo.TreeQueryVo;
import com.indexingsystem.boss.entity.ResourceCategors;
import com.indexingsystem.boss.logback.LogCvt;
import com.indexingsystem.db.mongoDB.MongoManager;
import com.indexingsystem.db.mongoDB.comon.MongoDBUtils;
import com.indexingsystem.db.mongoDB.comon.MongoTableName;
import com.mongodb.BasicDBObject;
import com.mongodb.client.model.Filters;

/**
 * 
 * @author Administrator 资源分类id获取资源列表 集合名称：dh_resource_categorys_tree
 *         集合说明：对产品分类中产品列表信息进行冗余，便于在产品分类中获取产品信息避免多表连接。
 * 
 * 
 */
public class ResourceCategorsMongo {

	public static void addResourceCategors(List<ResourceCategorsVo> resourceCategorsVoList, String institutionId) {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_RESOURCE_CATEGORYS_TREE + "添加 ------->"
				+ com.alibaba.fastjson.JSON.toJSONString( resourceCategorsVoList, true ) );
		List<Document> addObjList = new ArrayList<Document>();
		if (null != resourceCategorsVoList && resourceCategorsVoList.size() > 0) {
			for (ResourceCategorsVo resourceCategorsVo : resourceCategorsVoList) {
				Document addObj = new Document();
				addObj.put( "institutionId", institutionId );
				addObj.put( "resName", resourceCategorsVo.getResName() );
				addObj.put( "description", resourceCategorsVo.getDescription() );
				addObj.put( "isPublications", resourceCategorsVo.getIsPublications() );
				addObj.put( "parentId", resourceCategorsVo.getParentId() );
				addObj.put( "resCategoryId", resourceCategorsVo.getResCategoryId() );
				addObj.put( "categoryOrder", resourceCategorsVo.getCategoryOrder() );
				addObjList.add( addObj );
			}

		}
		manager.addList( addObjList, MongoTableName.DH_RESOURCE_CATEGORYS_TREE ); // 向mongodb插入数据

	}

	/**
	 * 获取资源分类树形数据.
	 * 
	 * @param map
	 * @return
	 */
	public static List<ResourceCategorsVo> findResourceCategorsAll(Map<String, Object> map, String institutionId) {
		MongoManager manager = new MongoManager();
		List<ResourceCategorsVo> resourceCategorsVoList = null;
		LogCvt.debug( MongoTableName.DH_RESOURCE_CATEGORYS_TREE + "查资源分类树形 ------->" );
		Bson bson = Filters.and( Filters.eq( "institutionId", institutionId ) );
		List<Document> documentsList = manager.queryAggregateList( bson, ResourceCategorsVo.class, MongoTableName.DH_RESOURCE_CATEGORYS_TREE );
		try {
			if (null != documentsList && documentsList.size() > 0) {
				resourceCategorsVoList = new ArrayList<ResourceCategorsVo>();
				for (Document document : documentsList) {
					ResourceCategorsVo resourceCategorsVo = new ResourceCategorsVo();
					resourceCategorsVo = (ResourceCategorsVo) MongoDBUtils.setFieldValue( resourceCategorsVo, document );
					if (!resourceCategorsVoList.contains( resourceCategorsVo )) {
						resourceCategorsVoList.add( resourceCategorsVo );
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		LogCvt.debug( "查询 ResourceCategorsVo 结果:" + JSON.toJSONString( resourceCategorsVoList, true ) );
		return resourceCategorsVoList;
	}

	/**
	 * 获取资源分类树形数据. 分页
	 * 
	 * @param map
	 * @return
	 */
	public static List<ResourceCategorsVo> findResourceCategorsByPage(TreeQueryVo treeReqVo, String institutionId) {
		MongoManager manager = new MongoManager();
		List<ResourceCategorsVo> resourceCategorsVoList = null;
		LogCvt.debug( MongoTableName.DH_RESOURCE_CATEGORYS_TREE + "查资源分类树形 ------->" );

		BasicDBObject basicDBObject = new BasicDBObject();
		basicDBObject.put( "parentId", treeReqVo.getId() );
		basicDBObject.put( "institutionId", institutionId );
		if (StringUtils.isNotBlank( treeReqVo.getQueryCriteria() )) {
			Pattern pattern = Pattern.compile( ".*" + treeReqVo.getQueryCriteria() + ".*", Pattern.CASE_INSENSITIVE );
			basicDBObject.put( "resName", pattern );
		}

		// 排序条件
		Document sortDocument = new Document();
		sortDocument.put( "categoryOrder", 1 );

		// 不显示的字段
		Document fetchFields = new Document();
		fetchFields.put( "_id", 0 );

		List<Document> documentsList = manager.queryPages( MongoTableName.DH_RESOURCE_CATEGORYS_TREE, basicDBObject, sortDocument, fetchFields,
				treeReqVo.getPageNumber(), treeReqVo.getPageSize() );
		try {
			if (null != documentsList && documentsList.size() > 0) {
				resourceCategorsVoList = new ArrayList<ResourceCategorsVo>();
				for (Document document : documentsList) {
					ResourceCategorsVo resourceCategorsVo = new ResourceCategorsVo();
					resourceCategorsVo = (ResourceCategorsVo) MongoDBUtils.setFieldValue( resourceCategorsVo, document );
					if (!resourceCategorsVoList.contains( resourceCategorsVo )) {
						resourceCategorsVoList.add( resourceCategorsVo );
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		LogCvt.debug( "查询 ResourceCategorsVo 结果:" + JSON.toJSONString( resourceCategorsVoList, true ) );
		return resourceCategorsVoList;
	}

	/**
	 * 获取机构树形数据.
	 * 
	 * @param map
	 * @return
	 */
	public static void deleteResourceCategorsAll(String institutionId) {
		MongoManager manager = new MongoManager();
		Bson bson = Filters.and( Filters.eq( "institutionId", institutionId ) );
		manager.deleteAll( bson, MongoTableName.DH_RESOURCE_CATEGORYS_TREE );
	}

	/**
	 * 获取机构树形数据.
	 * 
	 * @param map
	 * @return
	 */
	public static void deleteResourceCategorsByIds(List<String> resCategoryIdList, String institutionId) {
		MongoManager manager = new MongoManager();
		Bson bson = Filters.and( Filters.in( "resCategoryId", resCategoryIdList ), Filters.eq( "institutionId", institutionId ) );
		manager.deleteAll( bson, MongoTableName.DH_RESOURCE_CATEGORYS_TREE );
	}

	/**
	 * 根据条件更新
	 * 
	 * @param map
	 * @return
	 */
	public static void updateResourceCategorsById(ResourceCategors record, String institutionId) {
		MongoManager manager = new MongoManager();
		Bson bson = Filters.and( Filters.eq( "resCategoryId", record.getResCategoryId() ), Filters.eq( "institutionId", institutionId ) );
		Bson doc = new Document( "$set", new Document( "resName", record.getResName() ).append( "description", record.getDescription() ) );
		manager.updateOne( bson, doc, MongoTableName.DH_RESOURCE_CATEGORYS_TREE );

	}
}